package com.example.gateway.config;

import cn.dev33.satoken.stp.StpInterface;
import com.example.commons.core.bean.UserInfoRightBean;
import com.example.commons.core.entity.Right;
import com.example.system.api.service.UserInfoDubboService;
import lombok.extern.log4j.Log4j2;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.spring.context.annotation.DubboComponentScan;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Component
@Log4j2
@DubboComponentScan
public class StpInterfaceImpl implements StpInterface {
    @DubboReference
    private UserInfoDubboService userInfoDubboService;

    @Override
    public List<String> getPermissionList(Object o, String s) {
        // 返回此 loginId 拥有的权限列表
        UserInfoRightBean userInfoRightBean = userInfoDubboService.rightAndUserFindByUserId(Long.parseLong(o.toString()));
        log.info(String.format("查询用户%s,的权限信息,来自接口:%s",userInfoRightBean.getUserName(),s));
        return userInfoRightBean.getRightList().stream().map(Right::getRightCode).collect(Collectors.toList());
    }

    @Override
    public List<String> getRoleList(Object o, String s) {
        // 返回此 loginId 拥有的角色列表
        List<String> roleList = new ArrayList<>();
        roleList.add("user");
        return roleList;
    }
}
